package com.iot.inclinometer.util;

import android.content.Context;

import com.iot.inclinometer.pojo.BaseInfo;
import com.iot.inclinometer.pojo.CorrectDataInfo;
import com.iot.inclinometer.pojo.DataInfo;
import com.iot.inclinometer.pojo.LeijiInfo;
import com.iot.inclinometer.pojo.ObserveInfo;
import com.iot.inclinometer.pojo.RealInfo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


/**
 * 导出数据
 * 
 * 
 */
public class JXLExportAll implements Runnable{

	private String _file=null;
	private	BaseInfo baseInfo=null;
	private List<List<BaseInfo>> baseInfoLists =null;
	private List<List<ObserveInfo>> observeInfoLists =null;
	private List<List<DataInfo>> dataInfoLists =null;
	private List<List<RealInfo>> realInfoLists =null;
	private List<List<LeijiInfo>> leijiInfoLists =null;
	private List<List<CorrectDataInfo>> correctDataInfoLists1=null;
	private Context context = null;
	private FileOutputStream fos = null;
	private WritableWorkbook wwb = null;
	private File fileDir  = null;
	private String filePath = null;
	private File targetFile = null;
	private String[] baseInfos ={"base_id","project_name","item_name","drill_num","depth","mark1","mark2","mark3","mark4","loc","x","y","azimuth"};
	private String[] observeInfos ={"ob_id","observer","time","interval","measurecount","slipdeep","base_id","count"};
	private String[] dataInfos ={"data_id","depth","lx","ly","error","theta_x","theta_y","theta","ob_id"};
	private String[] leijidataInfos ={"leiji_id","depth","lx","ly","theta_x","theta_y","ob_id"};
	private String[] correctDataInfos ={"data_id","depth","lx","ly","error","theta_x","theta_y","theta","ob_id",
										"A0","B0","A180","B180"};
	private String[] realInfos ={"real_id","theta_A","theta_B","ltheta_A","ltheta_B","lltheta_A",
			                   "lltheta_B","currentMeasureTag","ob_id"};
	//备份文件里的角度的单位：弧度	
	public JXLExportAll(String file,BaseInfo baseInfo,List<List<CorrectDataInfo>> correctDataInfoLists1,List<List<DataInfo>> dataInfoLists,List<List<LeijiInfo>> leijiInfoLists,List<List<ObserveInfo>> observeInfoLists,List<List<BaseInfo>> baseInfoLists,List<List<RealInfo>> realInfoLists,Context context) {
		this._file=file;
		this.baseInfo = baseInfo;
		this.baseInfoLists=baseInfoLists;
		this.observeInfoLists=observeInfoLists;
		this.dataInfoLists=dataInfoLists;
		this.leijiInfoLists=leijiInfoLists;
		this.correctDataInfoLists1= correctDataInfoLists1;	
		this.realInfoLists=realInfoLists;
		this.context = context;
	}
	
	public void run() {	
		try {
			// 准备设置excel工作表的标题     // 输出的excel的路径
			fileDir = new File(_file);
			if (!fileDir.exists()) {
				fileDir.mkdirs();
			}
			Date now = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");//日期格式 
			String time = dateFormat.format(now);
			
			filePath =time+ "-"+"备份"+ ".xls"; // 定义导出文件
			
			if (new File(fileDir, filePath).exists()) { // 如果这个文件存在，那么暂时不做处理

			} else {
				targetFile = new File(fileDir, filePath);
				// 创建Excel工作薄
				
				// 新建立一个jxl文件,即在SDcard盘下生成test.xls
				fos = new FileOutputStream(targetFile);
				
				wwb = Workbook.createWorkbook(fos);
				// 添加第一个工作表并设置第一个Sheet的名字
				WritableSheet sheet1= wwb.createSheet("钻孔信息", 0);
				WritableSheet sheet2= wwb.createSheet("观测信息", 0);
				WritableSheet sheet3= wwb.createSheet("数据信息", 0);
				WritableSheet sheet4= wwb.createSheet("修正数据信息", 0);
				WritableSheet sheet5= wwb.createSheet("累计数据信息", 0);
				WritableSheet sheet6= wwb.createSheet("实时数据信息", 0);
				
				//钻孔信息列的内容
				for (int i = 0; i < baseInfos.length; i++) {
					Label label;
					// Label(x,y,z)其中x代表单元格的第x+1列，第y+1行, 单元格的内容是y
					// 在Label对象的子对象中指明单元格的位置和内容
					label = new Label(i, 0, baseInfos[i]);
					// 将定义好的单元格添加到工作表中
					sheet1.addCell(label);					
				}
				
				//观测信息列的内容
				for (int i = 0; i < observeInfos.length; i++) {
					Label label;
					// Label(x,y,z)其中x代表单元格的第x+1列，第y+1行, 单元格的内容是y
					// 在Label对象的子对象中指明单元格的位置和内容
					label = new Label(i, 0, observeInfos[i]);
					// 将定义好的单元格添加到工作表中
					sheet2.addCell(label);					
				}
				
				//数据信息列的内容
				for (int i = 0; i < dataInfos.length; i++) {
					Label label;
					// Label(x,y,z)其中x代表单元格的第x+1列，第y+1行, 单元格的内容是y
					// 在Label对象的子对象中指明单元格的位置和内容
					label = new Label(i, 0, dataInfos[i]);
					// 将定义好的单元格添加到工作表中
					sheet3.addCell(label);					
				}
				
				//修正数据信息列的内容
				for (int i = 0; i < correctDataInfos.length; i++) {
					Label label;
					// Label(x,y,z)其中x代表单元格的第x+1列，第y+1行, 单元格的内容是y
					// 在Label对象的子对象中指明单元格的位置和内容
					label = new Label(i, 0, correctDataInfos[i]);
					// 将定义好的单元格添加到工作表中
					sheet4.addCell(label);					
				}
				//累计数据信息列的内容
				for (int i = 0; i < leijidataInfos.length; i++) {
					Label label;
					// Label(x,y,z)其中x代表单元格的第x+1列，第y+1行, 单元格的内容是y
					// 在Label对象的子对象中指明单元格的位置和内容
					label = new Label(i, 0, leijidataInfos[i]);
					// 将定义好的单元格添加到工作表中
					sheet5.addCell(label);					
				}
				//实时数据信息列的内容
				for (int i = 0; i < realInfos.length; i++) {
					Label label;
					// Label(x,y,z)其中x代表单元格的第x+1列，第y+1行, 单元格的内容是y
					// 在Label对象的子对象中指明单元格的位置和内容
					label = new Label(i, 0, realInfos[i]);
					// 将定义好的单元格添加到工作表中
					sheet6.addCell(label);					
				}
				
				//给钻孔信息列下的每行赋值
				for(List<BaseInfo> baseInfos :baseInfoLists){
				if(baseInfos.size()>0){				
				int length = baseInfos.size();
			//	System.out.println("数组长度========="+length);				
				int[] base_ids =new int[length];				
				String[] project_names =new String[length];
				String[] item_names=new String[length];
				String[] drill_nums=new String[length];
				String[] depths=new String[length];
				String[] mark1s=new String[length];
				String[] mark2s=new String[length];
				String[] mark3s=new String[length];
				String[] mark4s=new String[length];
				String[] locs=new String[length];
				String[] xs=new String[length];
				String[] ys=new String[length];
				String[] azimuths=new String[length];
				
				
					int j= 0;
					for (BaseInfo d : baseInfos) {
						base_ids [j]=(int)(d.getBaseId());
						project_names [j]=(String)(d.getProjectName());
						item_names [j]=(String)(d.getItemName());
						drill_nums [j]=(String)(d.getDrillNum());
						depths [j]=(String)(d.getDepth());
						mark1s [j]=(String)(d.getMark1());
						mark2s [j]=(String)(d.getMark2());
						mark3s [j]=(String)(d.getMark3());
						mark4s [j]=(String)(d.getMark4());
						locs [j]=(String)(d.getLoc());
						xs [j]=(String)(d.getX());
						ys [j]=(String)(d.getY());
						azimuths [j]=(String)(d.getAzimuth());	
						j++;
					}

				int lineNum = 1;
				for (int a = 0; a < length; a++) {
					Label label;
					jxl.write.Number base_id = new jxl.write.Number(0, lineNum,base_ids[a]);
					sheet1.addCell(base_id);	
					label = new Label(1, lineNum,project_names[a]);					
					sheet1.addCell(label);
					label = new Label(2, lineNum,item_names[a]);					
					sheet1.addCell(label);
					label = new Label(3, lineNum,drill_nums[a]);					
					sheet1.addCell(label);
					label = new Label(4, lineNum,depths[a]);					
					sheet1.addCell(label);
					label = new Label(5, lineNum,mark1s[a]);					
					sheet1.addCell(label);
					label = new Label(6, lineNum,mark2s[a]);					
					sheet1.addCell(label);
					label = new Label(7, lineNum,mark3s[a]);					
					sheet1.addCell(label);
					label = new Label(8, lineNum,mark4s[a]);					
					sheet1.addCell(label);
					label = new Label(9, lineNum,locs[a]);					
					sheet1.addCell(label);
					label = new Label(10, lineNum,xs[a]);					
					sheet1.addCell(label);
					label = new Label(11, lineNum,ys[a]);					
					sheet1.addCell(label);
					label = new Label(12, lineNum,azimuths[a]);					
					sheet1.addCell(label);										
					lineNum++;
				}
			}
					
		}
				//给观测信息列下的每行赋值
				for(List<ObserveInfo> observeInfos :observeInfoLists){
					if(observeInfos.size()>0){
						int length = observeInfos.size();	
						int[] ob_ids =new int[length];				
						String[] observers =new String[length];
						String[] times=new String[length];
						String[] intervals=new String[length];
						String[] measurecounts=new String[length];
						String[] slipdeeps=new String[length];
						int[] base_ids=new int[length];
						int[] counts=new int[length];									
						
							int j= 0;
							for (ObserveInfo d : observeInfos) {
								ob_ids [j]=(int)(d.getObserveId());
								observers [j]=(String)(d.getObserver());
								times [j]=(String)(d.getTime());
								intervals [j]=(String)(d.getInterval());
								measurecounts [j]=(String)(d.getMeasurecount());
								slipdeeps [j]=(String)(d.getSlipdeep());
								base_ids [j]=(int)(d.getBaseId());
								counts [j]=(int)(d.getCount());								
								j++;
							}
							int lineNum = 1;
							for (int a = 0; a < length; a++) {
								Label label;
								jxl.write.Number ob_id = new jxl.write.Number(0, lineNum,ob_ids[a]);
								sheet2.addCell(ob_id);	
								label = new Label(1, lineNum,observers[a]);					
								sheet2.addCell(label);
								label = new Label(2, lineNum,times[a]);					
								sheet2.addCell(label);
								label = new Label(3, lineNum,intervals[a]);					
								sheet2.addCell(label);
								label = new Label(4, lineNum,measurecounts[a]);					
								sheet2.addCell(label);
								label = new Label(5, lineNum,slipdeeps[a]);					
								sheet2.addCell(label);
								jxl.write.Number base_id = new jxl.write.Number(6, lineNum,base_ids[a]);
								sheet2.addCell(base_id);						
								jxl.write.Number count = new jxl.write.Number(7, lineNum,counts[a]);
								sheet2.addCell(count);		
								lineNum++;
							}									
						}							
					}
				
				//给数据信息列下的每行赋值
				for(List<DataInfo> dataInfos :dataInfoLists){
					if(dataInfos.size()>0){
						int length = dataInfos.size();
						
						int[] data_ids =new int[length];				
						String[] depths =new String[length];
						String[] lxs=new String[length];
						String[] lys=new String[length];
						String[] errors=new String[length];
						String[] theta_xs=new String[length];
						String[] theta_ys=new String[length];
						String[] thetas=new String[length];
						int[] ob_ids=new int[length];
						
							int j= 0;
							for (DataInfo d : dataInfos) {								
								data_ids [j]=(int)(d.getDataId());
								depths [j]=(String)(d.getDepth());								
								lxs [j]=(String)(d.getLx());
								lys [j]=(String)(d.getLy());
								errors [j]=(String)(d.getError());
								theta_xs [j]=(String)(d.getThetaX());
								theta_ys [j]=(String)(d.getThetaY());
								thetas [j]=(String)(d.getTheta());	
								ob_ids [j]=(int)(d.getObserverId());	
								j++;
							}
							int lineNum = 1;
							for (int a = 0; a < length; a++) {
								Label label;
								jxl.write.Number data_id = new jxl.write.Number(0, lineNum,data_ids[a]);
								sheet3.addCell(data_id);
								label = new Label(1, lineNum,depths[a]);					
								sheet3.addCell(label);
								label = new Label(2, lineNum,lxs[a]);					
								sheet3.addCell(label);
								label = new Label(3, lineNum,lys[a]);					
								sheet3.addCell(label);
								label = new Label(4, lineNum,errors[a]);					
								sheet3.addCell(label);
								label = new Label(5, lineNum,theta_xs[a]);					
								sheet3.addCell(label);
								label = new Label(6, lineNum,theta_ys[a]);					
								sheet3.addCell(label);
								label = new Label(7, lineNum,thetas[a]);					
								sheet3.addCell(label);
								jxl.write.Number ob_id = new jxl.write.Number(8, lineNum,ob_ids[a]);
								sheet3.addCell(ob_id);
								lineNum++;
							}					
						}
					}
				//给累计数据信息列下的每行赋值
				for(List<LeijiInfo> ljdataInfos :leijiInfoLists){
					if(ljdataInfos.size()>0){
						int length = ljdataInfos.size();
						
						int[] leiji_ids =new int[length];				
						String[] depths =new String[length];
						String[] lxs=new String[length];
						String[] lys=new String[length];
						String[] theta_xs=new String[length];
						String[] theta_ys=new String[length];
						int[] ob_ids=new int[length];
						
							int j= 0;
							for (LeijiInfo d : ljdataInfos) {								
								leiji_ids [j]=(int)(d.getLeijiId());
								depths [j]=(String)(d.getDepth());								
								lxs [j]=(String)(d.getLx());
								lys [j]=(String)(d.getLy());
								theta_xs [j]=(String)(d.getThetaX());
								theta_ys [j]=(String)(d.getThetaY());	
								ob_ids [j]=(int)(d.getObserverId());	
								j++;
							}
							int lineNum = 1;
							for (int a = 0; a < length; a++) {
								Label label;
								jxl.write.Number data_id = new jxl.write.Number(0, lineNum,leiji_ids[a]);
								sheet5.addCell(data_id);
								label = new Label(1, lineNum,depths[a]);					
								sheet5.addCell(label);
								label = new Label(2, lineNum,lxs[a]);					
								sheet5.addCell(label);
								label = new Label(3, lineNum,lys[a]);					
								sheet5.addCell(label);	
								label = new Label(4, lineNum,theta_xs[a]);					
								sheet5.addCell(label);
								label = new Label(5, lineNum,theta_ys[a]);					
								sheet5.addCell(label);
								jxl.write.Number ob_id = new jxl.write.Number(6, lineNum,ob_ids[a]);
								sheet5.addCell(ob_id);
								lineNum++;
							}					
						}
					}
				//给修正数据信息列下的每行赋值
				for(List<CorrectDataInfo> correctDataInfos :correctDataInfoLists1){
					if(correctDataInfos.size()>0){
						int length = correctDataInfos.size();
						int[] data_ids =new int[length];				
						String[] depths =new String[length];
						String[] lxs=new String[length];
						String[] lys=new String[length];
						String[] errors=new String[length];
						String[] theta_xs=new String[length];
						String[] theta_ys=new String[length];
						String[] thetas=new String[length];					
						int[] ob_ids=new int[length];
						String[] A0s=new String[length];
						String[] B0s=new String[length];
						String[] A180s=new String[length];
						String[] B180s=new String[length];						
								
						int j= 0;
						for (CorrectDataInfo d : correctDataInfos) {								
							data_ids [j]=(int)(d.getDataId());
							depths [j]=(String)(d.getDepth());								
							lxs [j]=(String)(d.getLx());
							lys [j]=(String)(d.getLy());
							errors [j]=(String)(d.getError());
							theta_xs [j]=(String)(d.getThetaX());
							theta_ys [j]=(String)(d.getThetaY());
							thetas [j]=(String)(d.getTheta());								
							ob_ids [j]=(int)(d.getObserverId());
							A0s [j]=(String)(d.getA0());
							B0s [j]=(String)(d.getB0());
							A180s [j]=(String)(d.getA180());
							B180s [j]=(String)(d.getB180());							
							j++;
						}
						int lineNum = 1;
						for (int a = 0; a < length; a++) {
							Label label;
							jxl.write.Number data_id = new jxl.write.Number(0, lineNum,data_ids[a]);
							sheet4.addCell(data_id);
							label = new Label(1, lineNum,depths[a]);					
							sheet4.addCell(label);
							label = new Label(2, lineNum,lxs[a]);					
							sheet4.addCell(label);
							label = new Label(3, lineNum,lys[a]);					
							sheet4.addCell(label);
							label = new Label(4, lineNum,errors[a]);					
							sheet4.addCell(label);
							label = new Label(5, lineNum,theta_xs[a]);					
							sheet4.addCell(label);
							label = new Label(6, lineNum,theta_ys[a]);					
							sheet4.addCell(label);
							label = new Label(7, lineNum,thetas[a]);					
							sheet4.addCell(label);							
							jxl.write.Number ob_id = new jxl.write.Number(8, lineNum,ob_ids[a]);
							sheet4.addCell(ob_id);
							label = new Label(9, lineNum,A0s[a]);					
							sheet4.addCell(label);
							label = new Label(10, lineNum,B0s[a]);					
							sheet4.addCell(label);
							label = new Label(11, lineNum,A180s[a]);					
							sheet4.addCell(label);
							label = new Label(12, lineNum,B180s[a]);					
							sheet4.addCell(label);
							
							lineNum++;
						}
					}
				}	
				//给实时数据信息列下的每行赋值
				for(List<RealInfo> realInfos :realInfoLists){
					if(realInfos.size()>0){
						int length = realInfos.size();
						int[] real_ids =new int[length];				
						String[] theta_As =new String[length];
						String[] theta_Bs=new String[length];
						String[] ltheta_As=new String[length];
						String[] ltheta_Bs=new String[length];
						String[] lltheta_As=new String[length];
						String[] lltheta_Bs=new String[length];
						String[] currentMeasureTags=new String[length];
						int[] ob_ids=new int[length];
						
							int j= 0;
							for (RealInfo d : realInfos) {								
								real_ids [j]=(int)(d.getRealId());
								theta_As [j]=(String)(d.getTheta_A());
								theta_Bs [j]=(String)(d.getTheta_B());								
								ltheta_As [j]=(String)(d.getLtheta_A());
								ltheta_Bs [j]=(String)(d.getLtheta_B());
								lltheta_As [j]=(String)(d.getLltheta_A());
								lltheta_Bs [j]=(String)(d.getLltheta_B());
								currentMeasureTags [j]=(String)(d.getCurrentMeasureTag());
								ob_ids [j]=(int)(d.getObserverId());	
								j++;
							}
							int lineNum = 1;
							for (int a = 0; a < length; a++) {
								Label label;
								jxl.write.Number real_id = new jxl.write.Number(0, lineNum,real_ids[a]);
								sheet6.addCell(real_id);
								label = new Label(1, lineNum,theta_As[a]);					
								sheet6.addCell(label);
								label = new Label(2, lineNum,theta_Bs[a]);					
								sheet6.addCell(label);
								label = new Label(3, lineNum,ltheta_As[a]);					
								sheet6.addCell(label);
								label = new Label(4, lineNum,ltheta_Bs[a]);					
								sheet6.addCell(label);
								label = new Label(5, lineNum,lltheta_As[a]);					
								sheet6.addCell(label);
								label = new Label(6, lineNum,lltheta_Bs[a]);					
								sheet6.addCell(label);
								label = new Label(7, lineNum,currentMeasureTags[a]);					
								sheet6.addCell(label);
								jxl.write.Number ob_id = new jxl.write.Number(8, lineNum,ob_ids[a]);
								sheet6.addCell(ob_id);
								lineNum++;
							}					
						}
					}
				// 写入数据
				wwb.write();
				// 关闭文件
			    wwb.close();
			}
		} catch (Exception e) {
			System.out.println(e);
		} finally {
			if (fos != null) {
				try {
					fos.close();
					/*targetFile.setWritable(false, true);//设置文件为只读
					if (targetFile.canWrite()) {
					   	Log.v(filePath, "This file is writable");
			        } else {
			        	Log.v(filePath, "This file is read only");
			        }*/
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	
}
